﻿using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Windows;
using Silverlight.Training.Client.Common;
using Silverlight.Training.Client.Common.ServiceReference.Contracts;
using Silverlight.Training.Client.Common.ServiceReference.EventArgs;
using SilverLight.Training.Models;

namespace SilverLight.Training.ViewModels
{
    [Export]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class DepartmentDetailsViewModel : NotifyPropertyChangedBase
    {
        private DepartmentDetailsModel model;

        [ImportingConstructor]
        public DepartmentDetailsViewModel()
        {
            model = new DepartmentDetailsModel();
            model.GetDepartmentCollectionCompleted += model_GetDepartmentCollectionCompleted;
            model.GetDepartmentCollectionAsync();
        }

        void model_GetDepartmentCollectionCompleted(object sender, CollectionFeedbackCompletedEventArgs<Department> e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                    DepartmentCollection = e.Result.Data;
                else
                {
                    MessageBox.Show("No result.");
                }
            }
            else 
            {
                MessageBox.Show("Error: " + e.Error);
            }
        }

        private ObservableCollection<Department> departmentCollection;
        public ObservableCollection<Department> DepartmentCollection
        {
            get { return departmentCollection; }
            set
            {
                if (!ReferenceEquals(value, departmentCollection))
                {
                    departmentCollection = value;
                    RaisePropertyChanged("DepartmentCollection");
                }
            }
        }
    }
}
